export class ByteUtil {

    constructor() {
    }

    static stringToByte(sourceStr, fixed = 2) {
        if (typeof sourceStr !== 'string') {
            return '0Byte';
        }
        const textEncoder = new TextEncoder();
        const value = textEncoder.encode(sourceStr).length;
        // use 1000 not 1024. 
        const kb = 1000;
        const mb = kb * 1000;
        const gb = mb * 1000;
        const tb = gb * 1000;
        if (value > tb) {
            return Number(value / tb).toFixed(fixed) + 'TB';
        } else if (value > gb) {
            return Number(value / gb).toFixed(fixed) + 'GB';
        } else if (value > mb) {
            return Number(value / mb).toFixed(fixed) + 'MB';
        } else if (value > kb) {
            return Number(value / kb).toFixed(fixed) + 'KB';
        } else {
            return value + 'Byte';
        }
    }
}
